package com.dashan.looperpager.card;

import android.os.Bundle;
import android.widget.RelativeLayout;

import androidx.appcompat.app.AppCompatActivity;

import com.dashan.looperpager.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: https://blog.csdn.net/qian520ao/article/details/68952079
 * @copyRight: 厦门立林科技有限公司
 * @author: yanxun
 * @createTime: 2022/3/31
 *
 */
public class CardMainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_card);

        initImgList();

        int mWidth = getWindowManager().getDefaultDisplay().getWidth();
        //高是宽的 0.565 ,根据图片比例
        float heightRatio = 0.565f;

        CardPagerAdapter cardAdapter = new CardPagerAdapter();
        cardAdapter.addImgUrlList(sPics);

        //设置阴影大小，即vPage  左右两个图片相距边框  maxFactor + 0.3*CornerRadius*2
        //设置阴影大小，即vPage 上下图片相距边框  maxFactor*1.5f + 0.3*CornerRadius
        int maxFactor = mWidth / 25;
        cardAdapter.setMaxElevationFactor(maxFactor);

        int mWidthPadding = mWidth / 8;

        //因为我们adapter里的cardView CornerRadius已经写死为10dp，所以0.3*CornerRadius=3
        //设置Elevation之后，控件宽度要减去 (maxFactor + dp2px(3)) * heightRatio
        //heightMore 设置Elevation之后，控件高度 比  控件宽度* heightRatio  多出的部分
        float heightMore = (1.5f * maxFactor + dp2px(3)) - (maxFactor + dp2px(3)) * heightRatio;
        int mHeightPadding = (int) (mWidthPadding * heightRatio - heightMore);

        CardViewPager viewPager = findViewById(R.id.view_page);
        viewPager.setLayoutParams(new RelativeLayout.LayoutParams(mWidth, (int) (mWidth * heightRatio)));
        viewPager.setPadding(mWidthPadding, mHeightPadding, mWidthPadding, mHeightPadding);
        viewPager.setClipToPadding(false);
        viewPager.setAdapter(cardAdapter);
        viewPager.setCurrentItem(cardAdapter.getDataRealSize() * 20, false);
        viewPager.showTransformer(0.2f);
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public int dp2px(float dpValue) {
        final float scale = getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    private List<Integer> sPics;

    public void initImgList() {
        sPics = new ArrayList<>();
        sPics.add(R.drawable.standby_weather);
        sPics.add(R.drawable.standby_clock);
        sPics.add(R.drawable.standby_time);
    }

}
